###############################################################################
#
# Copyright (c) 2011 Rainmaker Entertainment
# All Rights Reserved.
#
# This file contains unpublished confidential and proprietary
# information of Rainmaker Entertainment.  The contents of this file
# may not be copied or duplicated, in whole or in part, by any
# means, electronic or hardcopy, without the express prior
# written permission of Rainmaker Entertainment.
#
#    $HeadURL: http://pt/src/trunk/maya/outsource/rnkOutsourceChecker/operations/core.py $
#    $Revision: 65859 $
#    $Author: nkoubi $
#    $Date: 2013-07-11 15:53:19 -0700 (Thu, 11 Jul 2013) $
#
###############################################################################

###############################################################################
## INFOS
###############################################################################

# Functions to be used by different checks.


###############################################################################
## MODULES
###############################################################################

import traceback

try :
    import maya.cmds as cmds
    import maya.mel as mel
    import maya.OpenMaya as OpenMaya
except :
    traceback.print_exc()
    print "Can't load Maya modules."


###############################################################################
## FUNCTIONS
###############################################################################   
def checkResult(**kw):
    
    d = { 'allowPublish' : None,
          'msg'          : '',
          'nodes'        : [],
          'warning'      : False,
        }

    d.update( kw )

    assert( d['allowPublish'] is True or d['allowPublish'] is False )
    assert( isinstance( d['msg'], basestring ) )
    assert( type(d['nodes']) is list )
    assert( d['warning'] is True or d['warning'] is False )

    return d


def getCtrlList(assetRoot):
    """ 
    @brief Get a list of nodes in the controllers set
    @return : List
    """
    
    ctrlList = []
    
    controllersSets = ['set_ctrl']
    for eachSet in controllersSets :
    
        ## Check if the controllers set exists
        if cmds.objExists(eachSet) is True :
            
            ## Get nodes under it
            getSet = cmds.sets(eachSet, q=True)
            if not getSet is None :
                
                ## Append every nodes who are not in the exclusion list to ctrlList
                for each in getSet :
                    if not cmds.nodeType(each) in ['set'] :
                        ctrlList.append(each)
    
    
    return ctrlList


    
def listStorableAttrs(node, shortName=True, noCompound=True):
    """ 
    @brief List every attributes of a node who can go trough the pipeline and whose value will be stored.
    @return : List
    
    @param node - String - Node to get the attributes of.
    @param shortName - Bool - If True, return attributes short name.
    @param noCompound - Bool - If True, exclude compound attributes.
    """
    
    def __getShortName(node, attr, shortName):
        result = attr
        if shortName:
            names = attr.split('.')
            snames = []
            for name in names:
                if name.endswith(']'):
                    snames.append(name)
                else:
                    snames.append(cmds.attributeQuery(name, node=node, shortName=True))
            result = ".".join(snames)

        return result

    result = []
    nodeType = cmds.nodeType(node)
    if not cmds.listAnimatable(node):
        return result

    for s in cmds.listAnimatable(node):
        if noCompound:
            if len(s.split('.')) > 2:
                continue

        nodeName = s.split('.')[0]
        attr = ".".join(s.split('.')[1:])

        ## Remove attributes on the shapes (listAnimatable return them)
        if cmds.nodeType(node) == cmds.nodeType(nodeName):
            attr = __getShortName(node, attr, shortName)
            result.append(str(attr))

    attrs = cmds.listAttr(node, cb=True)
    if attrs:
        for attr in attrs:
            attr = __getShortName(node, attr, shortName)
            result.append(str(attr))

    if cmds.listAttr(node, userDefined=True):
        extra = [str(x) for x in cmds.listAttr(node, userDefined=True) if cmds.getAttr("%s.%s" % (node, x), type=True) == 'string' if not x.startswith("rnkInit_") if not cmds.getAttr("%s.%s" % (node, x), lock=True)]
        if extra:
            result.extend(extra)

    return result